package com.sjy.shopmall.controller;

import com.sjy.shopmall.dao.CartMapper;
import com.sjy.shopmall.dao.OrderItemMapper;
import com.sjy.shopmall.dao.OrderMapper;
import com.sjy.shopmall.enums.OrderStatusEnum;
import com.sjy.shopmall.pojo.Cart;
import com.sjy.shopmall.pojo.OrderDO;
import com.sjy.shopmall.pojo.OrderItem;
import com.sjy.shopmall.utils.SysUtils;
import com.sjy.shopmall.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    OrderMapper orderMapper;

    @Resource
    OrderItemMapper orderItemMapper;

    @Resource
    SysUtils sysUtils;

    @Resource
    CartMapper cartMapper;

    @PostMapping("/new")
    public ResultVO newOrder(@RequestBody OrderCreateForm data) {
        Long userid = sysUtils.getLoginedUserID();
        if (userid == null) {
            throw new RuntimeException("请登陆后重试");
        }
        OrderDO orderDO = new OrderDO();
        orderDO.setAddress(data.getAddress());
        orderDO.setCreateDate(new Date());
        orderDO.setStatus(OrderStatusEnum.CREATED.getVal());
        orderDO.setUserId(userid);
        orderMapper.insert(orderDO);
        for (CartDTO d : data.getCarts()) {
            if (d == null) {
                continue;
            }
            OrderItem item = new OrderItem();
            item.setGoodCount(d.getGoodCount());
            item.setGoodId(d.getGoodId());
            item.setGoodPrice(d.getPrice());
            item.setOrderId(orderDO.getId());
            orderItemMapper.insert(item);
            cartMapper.deleteByPrimaryKey(d.getCartId());
        }
        return new ResultVO(ResultVO.success, null, orderDO);
    }

    @GetMapping("updateStatus")
    ResultVO order(int status, Long id) {
        OrderDO d = orderMapper.selectByPrimaryKey(id);
        d.setStatus(status);
        orderMapper.updateByPrimaryKey(d);
        return new ResultVO(ResultVO.success, null, d);
    }

    @PostMapping("updateComment")
    ResultVO order(@RequestBody OrderItemDTO orderItem) {
        OrderItem d = orderItemMapper.selectByPrimaryKey(orderItem.getId());
        d.setRate(orderItem.getRate());
        d.setBuyerComment(orderItem.getBuyerComment());
        d.setCommentDate(new Date());
        orderItemMapper.updateByPrimaryKey(d);
        return new ResultVO(ResultVO.success, null, d);
    }

    @GetMapping("listComments")
    ResultVO listComments(Long goodId)
    {
        CommentDTO commentDTO=new CommentDTO();
        commentDTO.setAvgRate(orderItemMapper.selectAvgRate(goodId));
        commentDTO.setCommentInfoList(orderItemMapper.selectComments(goodId));
        return new ResultVO(ResultVO.success,null,commentDTO);
    }

    @GetMapping("list")
    ResultVO order() {
        Long userid = sysUtils.getLoginedUserID();
        if (userid == null) {
            throw new RuntimeException("请登陆后重试");
        }
        OrderDO d = new OrderDO();
        d.setUserId(userid);
        List<OrderDO> list = orderMapper.select(d);
        List<OrderDTO> orderDTOS = new LinkedList<>();
        for (OrderDO o : list) {
            OrderDTO dto = new OrderDTO();
            dto.setOrder(o);
            dto.setList(orderItemMapper.selectByOrder(o.getId()));
            BigDecimal b = new BigDecimal(0);
            for (OrderItemDTO i : dto.getList()) {
                b = b.add(i.getGoodCount().multiply(i.getGoodPrice()));
            }
            b = b.setScale(2);
            dto.setTotal(b);
            orderDTOS.add(dto);
        }
        return new ResultVO(ResultVO.success, null, orderDTOS);
    }

}
